# @Author ZhangGJ
# @Date 2021/11/24 10:49

import cv2.cv2 as cv2
import numpy as np
import time


def draw_line():
    canvas = np.zeros((300, 300, 3), np.uint8)
    canvas = cv2.line(canvas, (50, 50), (250, 50), (255, 0, 0), 5)
    canvas = cv2.line(canvas, (50, 150), (250, 150), (0, 255, 0), 10)
    canvas = cv2.line(canvas, (50, 250), (250, 250), (0, 0, 255), 15)
    canvas = cv2.line(canvas, (150, 50), (150, 250), (0, 255, 255), 20)
    cv2.imshow('Lines', canvas)
    cv2.waitKey()
    cv2.destroyAllWindows()


def draw_rectangle():
    canvas = np.zeros((300, 300, 3), np.uint8)
    canvas = cv2.rectangle(canvas, (50, 50), (200, 150), (255, 255, 0), 20)
    # canvas = cv2.rectangle(canvas, (50, 50), (200, 150), (255, 255, 0), -1) #  实心
    cv2.imshow('Rectangle', canvas)
    cv2.waitKey()
    cv2.destroyAllWindows()


def draw_square():
    canvas = np.zeros((300, 300, 3), np.uint8)
    canvas = cv2.rectangle(canvas, (50, 50), (255, 255), (0, 0, 255), 40)
    canvas = cv2.rectangle(canvas, (90, 90), (210, 210), (0, 255, 0), 30)
    canvas = cv2.rectangle(canvas, (120, 120), (180, 180), (255, 0, 0), 20)
    canvas = cv2.rectangle(canvas, (140, 140), (160, 160), (0, 255, 255), -1)
    cv2.imshow('Square', canvas)
    cv2.waitKey()
    cv2.destroyAllWindows()


def draw_circle():
    canvas = np.zeros((100, 300, 3), np.uint8)
    canvas = cv2.circle(canvas, (50, 50), 40, (0, 0, 255), -1)
    canvas = cv2.circle(canvas, (150, 50), 40, (0, 255, 255), -1)
    canvas = cv2.circle(canvas, (250, 50), 40, (0, 255, 0), -1)
    cv2.imshow('Circle', canvas)
    cv2.waitKey()
    cv2.destroyAllWindows()


def draw_circle2():
    canvas = np.zeros((300, 300, 3), np.uint8)
    center_x = int(canvas.shape[1] / 2)
    center_y = int(canvas.shape[0] / 2)
    for r in range(0, 150, 30):
        cv2.circle(canvas, (center_x, center_y), r, (0, 255, 0), 5)
    cv2.imshow('Circle', canvas)
    cv2.waitKey()
    cv2.destroyAllWindows()


def draw_random_circle():
    canvas = np.zeros((300, 300, 3), np.uint8)
    for number in range(0, 28):
        center_x = np.random.randint(0, high=300)
        center_y = np.random.randint(0, high=300)
        radius = np.random.randint(11, high=71)
        color = np.random.randint(0, high=256, size=(3,)).tolist()
        cv2.circle(canvas, (center_x, center_y), radius, color, -1)
    cv2.imshow('Circle', canvas)
    cv2.waitKey()
    cv2.destroyAllWindows()


def draw_polygon():
    canvas = np.zeros((300, 300, 3), np.uint8)
    pts = np.array([[100, 50], [200, 50], [250, 250], [50, 250]], np.int32)
    # pts = np.array([[100, 50], [200, 50], [50, 250], [250, 250]], np.int32)
    canvas = cv2.polylines(canvas, [pts], True, (0, 0, 255), 5)
    # canvas = cv2.polylines(canvas, [pts], False, (0, 0, 255), 5)
    cv2.imshow('Polylines', canvas)
    cv2.waitKey()
    cv2.destroyAllWindows()


def draw_word():
    canvas = np.zeros((100, 300, 3), np.uint8)
    # cv2.putText(canvas, 'mrsoft', (20, 70), cv2.FONT_HERSHEY_TRIPLEX, 2, (0, 255, 0), 5)
    cv2.putText(canvas, 'mrsoft', (20, 70), cv2.FONT_HERSHEY_DUPLEX, 2, (0, 255, 0), 5)
    cv2.imshow('Text', canvas)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 斜体
def draw_word2():
    canvas = np.zeros((100, 300, 3), np.uint8)
    font_style = cv2.FONT_HERSHEY_TRIPLEX + cv2.FONT_ITALIC
    cv2.putText(canvas, 'mrsoft', (20, 70), font_style, 2, (0, 255, 0), 5)
    cv2.imshow('Text', canvas)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 垂直镜像效果
def draw_word3():
    canvas = np.zeros((200, 300, 3), np.uint8)
    font_style = cv2.FONT_HERSHEY_TRIPLEX
    cv2.putText(canvas, 'mrsoft', (20, 70), font_style, 2, (0, 255, 0), 5)
    cv2.putText(canvas, 'mrsoft', (20, 100), font_style, 2, (0, 255, 0), 5, 8, True)
    cv2.imshow('Text', canvas)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 图像上绘制文字
def draw_word4():
    image = cv2.imread('../images/flower.jpg')
    font_style = cv2.FONT_HERSHEY_TRIPLEX
    cv2.putText(image, 'Flower', (20, 90), font_style, 1, (0, 255, 255))
    cv2.imshow('Text', image)
    cv2.waitKey()
    cv2.destroyAllWindows()


# 绘制动态图像
def draw_dynamic_image():
    width, height = 200, 200
    r = 20
    x = r + 20
    y = r + 100
    x_offer = y_offer = 4
    while cv2.waitKey(1) == -1:
        if x > width - r or x < r:
            x_offer *= -1
        if y > height - r or y < r:
            y_offer *= -1
        x += x_offer
        y += y_offer
        img = np.ones((width, height, 3), np.uint8) * 255
        cv2.circle(img, (x, y), r, (255, 0, 0), -1)
        cv2.imshow('img', img)
        time.sleep(1 / 60)
    cv2.destroyAllWindows()


if __name__ == '__main__':
    draw_dynamic_image()
